package com.java8.stream;

import com.java8.lambda.bean.Employee;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author xuehu
 * @date 2020/12/1 10:06
 */

public class StreamPractiseTest {


    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99, Employee.Status.FREE),
            new Employee("李四", 38, 5555.55, Employee.Status.BUSY),
            new Employee("王五", 50, 6666.99, Employee.Status.VOCATION),
            new Employee("赵六", 16, 8888.99, Employee.Status.FREE),
            new Employee("田七", 8, 7777.99, Employee.Status.VOCATION)

    );


    /**
     * 1.给定一个数字列表，如何返回一个由每个数的平方构成的列表呢?
     * 给定[1,2,3,4,5],应该返回[1,4,9,16,25]。
     */
    @Test
    public void test1() {
        Integer[] nums = new Integer[]{1, 2, 3, 4, 5};

        Arrays.stream(nums).map(x -> x * x).forEach(System.out::println);

    }

    /**
     * 2.怎样用map和reduce方法数一数流中有多少个Empolyee呢?
     */

    @Test
    public void test2() {

        Optional<Long> reduce = employees.stream()
                .map(e -> 1L).reduce(Long::sum);

        System.out.println(reduce.get());
    }
}
